#include "test_runner.h"
#include "gtest/gtest.h"
#include "mylib.h"
#include <fstream>
#include <mylib/mylib.h>

class FileOutputListener : public testing::EmptyTestEventListener {
public:
    explicit FileOutputListener(const std::string& path) : file_path(path) {}
    
    void OnTestProgramEnd(const testing::UnitTest& unit_test) override {
        std::ofstream out(file_path);
        out << "===== 测试报告 =====\n";
        out << "总用例数: " << unit_test.total_test_count() << "\n";
        out << "通过用例: " << unit_test.successful_test_count() << "\n";
        out << "失败用例: " << unit_test.failed_test_count() << "\n\n";
        
        // 添加详细测试结果
        for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
            const testing::TestSuite* suite = unit_test.GetTestSuite(i);
            out << "[" << suite->name() << "]\n";
            for (int j = 0; j < suite->total_test_count(); ++j) {
                const testing::TestInfo* test = suite->GetTestInfo(j);
                out << "  " << test->name() << ": "
                    << (test->result()->Passed() ? "通过" : "失败") << "\n";
            }
        }
        out.close();
    }
private:
    std::string file_path;
};

// 测试用例保持不变
TEST(MathTest, AddTest) {
    EXPECT_EQ(C_Add(2, 3), 5);
    EXPECT_EQ(C_Add(-1, 1), 0);
}
// 测试用例保持不变
TEST(MathTest, SubTest) {
    EXPECT_EQ(C_Reduce(20, 3), 5);
}

// 修改后的接口：接收文件路径参数
extern "C" void RunAllTests(const char* outputPath) {
    auto& listeners = testing::UnitTest::GetInstance()->listeners();
    listeners.Append(new FileOutputListener(outputPath));
    testing::InitGoogleTest();
    RUN_ALL_TESTS();
}